// pages/balance/balance.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userMessage:"",
    balance:"",
    charge:"",
    username:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that=this
    wx.request({
      url: "http://localhost:8080/user/getById",
      method: "GET",
      header: {
        'content-type': 'application/json'
      },
      data: {
        username:app.globalData.username
      },
      // res.data表示成功的时候返回的数据
      success(res) {
        // console.log(res.data);
        if (res.data.code == 200) {
          that.setData({
           userMessage:res.data.data,
           balance:res.data.data.balance,
           username:res.data.data.username
          })
          app.globalData.userMessage=that.data.userMessage
          console.log(that.data.userMessage)
          console.log(app.globalData.userMessage)
          console.log(that.data.balance)
        }
      }
    });
  },

  charge(e){
    this.setData({
      charge:e.detail.value
    })
  },

  register(e){
    let that=this;
    console.log(that.data.username),
    console.log(that.data.charge),
    console.log(that.data.balance),
    wx.request({
      url: "http://localhost:8080/user/balance",
      method: "POST",
      header: {
        'content-type': 'application/json'
      },
      data: {
        username:that.data.username,
        price:that.data.charge,
        balance:that.data.balance
      },
      // res.data表示成功的时候返回的数据
      success(res) {
        // console.log(res.data);
        if (res.data.code == 200) {
          wx.showToast({
            title: '氪金成功',
            icon:"success",
            duration:1000
          })
        }
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function (e) {
    this.setData({
      charge:""
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})